<?php
use Thenbsp\Wechat\OAuth\Client;
use Thenbsp\Wechat\Wechat\Jsapi;
use Thenbsp\Wechat\Wechat\AccessToken;

class RunnerController extends Yaf_Controller_Abstract {
		
	const LIST_KEY = 'roomlist';
	const INFO_KEY = 'room:';
	const MAX_POS = 4;
	const DEFAULT_POINT = 10000;
	const MAX_POINT = 4;
	const VALID_TIME = 20;
	//玩家状态
	const STATE_NONE = 0;
	const STATE_READY = 1;
	const STATE_PLAYING = 2;
	const PLAYER_ORDER ='player_order::';
	const LIST_USER='userlist';
	const SUM="sum:";
	
	public function ageAction(){
		$this->redis=RedisDB::factory('user');
		$userlist = $this->redis->hGetAll(self::LIST_USER);
		if(is_array($userlist) && $userlist){
			foreach($userlist as $cur_uid=>$user){
				$userInfo =json_decode($user,true);
				$uid =$userInfo['uid'];
				$mUser = new UserModel($uid);
				$user_info=$mUser->getInfo();
				//print_r($user_info);
				if($uid==19||$uid==1||$uid==12||$uid=11||$uid=25){
					//$user_info['gold']=10000;
					// $user_info['roomid']='';
					// $user_info['seatid']='';
					// $mUser->save($user_info);
				}
							
				//
			}
		}
		// $matchModel =new MatchModel(19);
		// $s=$matchModel->getMacth();
		//print_r($s);exit;
		// exit;
		// $log =new LogModel('58450182',1);
		// $mUser =new UserModel(1);
		// print_r($mUser->getInfo());
		// $log->del();
		// if(!$log->isExist()){
		// 	echo 'bucuzai';
		// }else{
		// 	print_r($log->getUidByRoom());
		// }
		// $roomid='58450182';
		// Socket::sendToChannel("game-{$roomid}", "Pushment", "message", array(
		// 		'seatid' => $myseatid,
		// 	));
		$this->redis=RedisDB::factory('game');

		$roomid =$_GET['roomid'];
		$list = $this->redis->hGetAll(ElephantajiangModel::LIST_KEY );
		if(!$roomid){
			$roomlist = $this->redis->hGetAll(self::LIST_KEY);
			print_r($roomlist);
			exit;
		}			
		
		$dachu = $this->redis->hGetAll(self::INFO_KEY.$roomid);
		//$this->redis->hDel(self::INFO_KEY.$roomid);
		echo json_encode($dachu);exit;
	}
	public function matchAction(){
		$mMatch =new MatchModel(1);
		$s=$mMatch->getMacth();
		$out =json_decode($s['list']);
		print_r($s);
	}
	public function clearAction(){
		$this->redis = RedisDB::factory('user');
		$userlist = $this->redis->hGetAll(self::LIST_USER);
		if(is_array($userlist) && $userlist){
			foreach($userlist as $cur_uid=>$user){
				$userInfo =json_decode($user,true);
				
				$uid =$userInfo['uid'];
				$mUser = new UserModel($uid);
				$user_info=$mUser->getInfo();
				$user_info['roomid']='';
				$user_info['seatid']='';
				$mUser->save($user_info);
			}
		}
			
			$this->redis = RedisDB::factory('game');
			$roomlist = $this->redis->hGetAll(self::LIST_KEY);
			foreach ($roomlist as $key => $value) {
				$this->redis->hDel(self::LIST_KEY,$key);
				$this->redis->hDel(self::INFO_KEY.$key);
			}
			print_r($roomlist);exit;	
	}
	public function indexAction(){
		$openid ='oLu-Lw6USjQNKO6jfPMo0ZOfM6Fk';
        $time = time()+60*60*24*7;//7days
        setcookie ( 'openid', $openid, $time, "/" );
		$this->redis=RedisDB::factory('game');
		$roomid=$_GET['roomid'];
		//if(!$roomid) return $roomid;
		// $mUser = new UserModel(1);
		// $mGame = new ElephantajiangModel($mUser,$roomid);
		// $handpai =array(6,6,9,9,17,18,19,23,23,23,27,28,29,9);
		// sort($handpai);
		// print_r($handpai);
		// $s  = $mGame->checkHu($handpai);
		// print_r($s);exit;
		// $seatkey ="seat1";
		// $front = $room[$seatkey]['front'];
		// $satisfy_ok =array();
		// array_push($satisfy_ok, 6);
		// $mGame->sumscore(1,9,4,$satisfy_ok);
		// print_r($satisfy_ok);
		//$mGame->clearGameInfo(1);
		/* $userlist = $this->redis->hGetAll(self::LIST_USER);
		if(is_array($userlist) && $userlist){
			foreach($userlist as $cur_uid=>$user){
				$userInfo =json_decode($user,true);
				
				$uid =$userInfo['uid'];
				$mUser = new UserModel($uid);
				$user_info=$mUser->getInfo();
				$user_info['roomid']='';
				$user_info['seatid']='';
				$mUser->save($user_info);
			}
		}
			
			$roomlist = $this->redis->hGetAll(self::LIST_KEY);
			foreach ($roomlist as $key => $value) {
				$this->redis->hDel(self::LIST_KEY,$key);
			// } */
			// $this->redis = RedisDB::factory('game');
			// $request=$this->redis->GET(self::SUM.'4');
			// $this->redis->del(self::SUM.'4');
			// if($request){
			// 	return false;
			// }
			
			// echo 'sss';
			// $this->redis->SET(self::SUM.'4',1);
			// $this->redis->LPUSH(self::SUM.'4',11);
			// $this->redis->LPUSH(self::SUM.'4',12);
			// $this->redis->LPUSH(self::SUM.'4',14);
			// $this->redis->LPUSH(self::SUM.'4',20);
			
			// $lpop =$this->redis->RPOP( self::SUM.'4');
			// print_r($lpop);
			//$dachu = $this->redis->hGetAll(self::INFO_KEY.$_GET['roomid']);
			//echo json_encode($dachu);
			//print_r($roomlist);exit;	
	}
	public function wechatGameAction()
	{
		//$url ="http://www.nbpipixia.cn/game/index.html";
		$url =$_POST['url'];
		$config_obj=Yaf_Registry::get("config");
		$config_wechat=$config_obj->wechat->toArray();
		$config_database =$config_obj->database->config->toArray();
	    $appid =$config_wechat['appid'];
		$appsecret=$config_wechat['appsecret'];
		$client = new Client($appid, $appsecret);
		$accessToken = new AccessToken($appid,$appsecret);
		$new_redis =RedisDB::factory('accesstoken');
		$cacheDriver = new \Doctrine\Common\Cache\RedisCache();
		$cacheDriver->setRedis($new_redis);
		$accessToken->setCache($cacheDriver);
		$access_token =$accessToken->getTokenString();
		$jsapi = new Jsapi($accessToken);
		$jsapi->setCache($cacheDriver);
		$jsapi->url=$url;
		//$jsapi->addApi('onMenuShareTimeline')->addApi('onMenuShareAppMessage');
		$signPackage = $jsapi->getConfig(true);

		$data =array('success'=>0,'signPackage'=>$signPackage);
		$this->response(200,'游戏页面config信息',$data);
	}
	public function response($code,$message,$data){
		$res_data =array(
					'code'=>$code,
					'message'=>$message,
					'data'=>$data,	
				);
			echo json_encode($res_data);
			exit;
	}
}